# -*- coding:utf-8 -*-

import pymysql
import tushare as ts

def index_get():
    db = pymysql.connect('localhost','root','minicooper','stock_info',charset='utf8')
    cursor = db.cursor()

    sql_head = "INSERT INTO STOCK_INDEX (CODE,NAME) VALUES "

    index_data = ts.get_index()

    for i in range(len(index_data)):
        index_data_pickle = index_data.iloc[i]
        code = index_data_pickle['code']
        name = index_data_pickle['name']
        insert_data = (code,name)
        sql_head += str(insert_data) + ','

    sql = sql_head.rstrip(',')

    cursor.execute(sql)

    db.commit()

if __name__ == '__main__':
    index_get()










"""
import time

def check_num():
    db = pymysql.connect('localhost','root','minicooper','stock_info',charset='utf8')
    cursor = db.cursor()

    parse_table_sql = "SELECT CODE FROM STOCK_HIST"
    cursor.execute(parse_table_sql)
    result = cursor.fetchall()

    print(len(result))

    code = []
    for i in result:
        co = i[0]
        if co in code:
            pass
        else:
            code.append(co)
    print(len(code))

    db.close()

while True:
    check_num()
    time.sleep(60)

"""